package org.ww.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ww.common.constant.ProductConstant;
import org.ww.common.utils.PageUtils;
import org.ww.common.utils.Query;
import org.ww.product.dao.AttrAttrgroupRelationDao;
import org.ww.product.dao.AttrDao;
import org.ww.product.entity.AttrAttrgroupRelationEntity;
import org.ww.product.entity.AttrEntity;
import org.ww.product.entity.AttrGroupEntity;
import org.ww.product.entity.CategoryEntity;
import org.ww.product.service.AttrAttrgroupRelationService;
import org.ww.product.service.AttrGroupService;
import org.ww.product.service.AttrService;
import org.ww.product.service.CategoryService;
import org.ww.product.vo.AttrRespVo;
import org.ww.product.vo.AttrVo;

import java.util.*;
import java.util.stream.Collectors;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveVo(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity);
        this.save(attrEntity);
        if(attrVo.getAttrType().equals(ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) && attrVo.getAttrGroupId() != null){
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(attrVo.getAttrGroupId());
            attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
        }

    }

    @Override
    public PageUtils baseList(Map<String, Object> params, Long catelogId, String attrType) {
        LambdaQueryWrapper<AttrEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AttrEntity::getAttrType,"base".equalsIgnoreCase(attrType) ? ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() : ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
        if(catelogId != 0){
            wrapper.eq(AttrEntity::getCatelogId,catelogId);
        }
        String key = (String) params.get("key");
        if(StringUtils.isNotBlank(key)){
            wrapper.and(obj -> obj.eq(AttrEntity::getAttrId,key).or().like(AttrEntity::getAttrName,key));
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        PageUtils pageUtils = new PageUtils(page);

        List<AttrRespVo> attrRespVos = page.getRecords().stream()
                .map(item -> {
                    AttrRespVo respVo = new AttrRespVo();
                    BeanUtils.copyProperties(item, respVo);
                    //获取属性名称
                    if(item.getAttrType().equals(ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode())){
                        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.getOne(
                                Wrappers.lambdaQuery(AttrAttrgroupRelationEntity.class)
                                        .eq(AttrAttrgroupRelationEntity::getAttrId, item.getAttrId()));

                        if (!ObjectUtils.isEmpty(attrAttrgroupRelationEntity)) {
                            AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId());
                            respVo.setGroupName(attrGroupEntity.getAttrGroupName());
                        }
                    }

                    //获取分类名称
                    CategoryEntity categoryEntity = categoryService.getById(item.getCatelogId());
                    if (!ObjectUtils.isEmpty(categoryEntity)) {
                        respVo.setCatelogName(categoryEntity.getName());
                    }
                    return respVo;
                }).collect(Collectors.toList());

        pageUtils.setList(attrRespVos);
        return pageUtils;
    }

    @Override
    public AttrRespVo getInfoByAttrId(Long attrId) {
        AttrRespVo respVo = new AttrRespVo();
        AttrEntity attrEntity = baseMapper.selectById(attrId);
        BeanUtils.copyProperties(attrEntity,respVo);

        if(attrEntity.getAttrType().equals(ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode())){
            //获取属性名称
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.getOne(
                    Wrappers.lambdaQuery(AttrAttrgroupRelationEntity.class)
                            .eq(AttrAttrgroupRelationEntity::getAttrId, attrId));

            if (!ObjectUtils.isEmpty(attrAttrgroupRelationEntity)) {
                AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId());
                if(!ObjectUtils.isEmpty(attrGroupEntity)){
                    respVo.setAttrGroupId(attrGroupEntity.getAttrGroupId());
                    respVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }
        }

        //获取分类名称
        CategoryEntity categoryEntity = categoryService.getById(attrEntity.getCatelogId());
        if (!ObjectUtils.isEmpty(categoryEntity)) {
            respVo.setCatelogName(categoryEntity.getName());
        }
        //获取分类路径
        Long[] catelogPath = categoryService.findCatelogPath(attrEntity.getCatelogId());
        respVo.setCatelogPath(catelogPath);
        return respVo;
    }

    @Override
    public void updateAttr(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity);

        this.updateById(attrEntity);

        if(attrVo.getAttrType().equals(ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode())){
            //判断是新增还是修改
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(attrVo.getAttrGroupId());

            LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(AttrAttrgroupRelationEntity::getAttrId,attrVo.getAttrId());
            Integer count = attrAttrgroupRelationDao.selectCount(wrapper);
            if(count > 0){
                //修改
                attrAttrgroupRelationService.update(attrAttrgroupRelationEntity,wrapper);
            }else{
                //新增
                attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
            }
        }

    }

    @Override
    public List<AttrEntity> getByAttrGroupId(Long attrgroupId) {
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AttrAttrgroupRelationEntity::getAttrGroupId,attrgroupId);
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationService.list(wrapper);

        List<Long> attrIds = Optional.ofNullable(attrAttrgroupRelationEntities)
                .orElseGet(ArrayList::new)
                .stream()
                .map(AttrAttrgroupRelationEntity::getAttrId)
                .distinct()
                .collect(Collectors.toList());

        LambdaQueryWrapper<AttrEntity> attrWrapper = Wrappers.lambdaQuery();
        attrWrapper.in(AttrEntity::getAttrId,attrIds);
        return baseMapper.selectList(attrWrapper);
    }

    @Override
    public List<AttrEntity> getByAttrGroupIds(List<Long> attrGroupIds) {
        if(CollectionUtils.isEmpty(attrGroupIds)){
            return Collections.emptyList();
        }
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AttrAttrgroupRelationEntity::getAttrGroupId,attrGroupIds);
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationService.list(wrapper);

        List<Long> attrIds = Optional.ofNullable(attrAttrgroupRelationEntities)
                .orElseGet(ArrayList::new)
                .stream()
                .map(AttrAttrgroupRelationEntity::getAttrId)
                .distinct()
                .collect(Collectors.toList());

        LambdaQueryWrapper<AttrEntity> attrWrapper = Wrappers.lambdaQuery();
        attrWrapper.in(AttrEntity::getAttrId,attrIds);
        return baseMapper.selectList(attrWrapper);
    }

    @Override
    public List<AttrEntity> getByIds(List<Long> attrIds) {
        LambdaQueryWrapper<AttrEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(AttrEntity::getAttrId,attrIds);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Long> selectSearchAttrIds(List<Long> attrIds) {
        LambdaQueryWrapper<AttrEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AttrEntity::getShowDesc,1);
        wrapper.in(AttrEntity::getAttrId,attrIds);
        List<AttrEntity> attrEntityList = baseMapper.selectList(wrapper);
        List<Long> searchAttrIds = Optional.ofNullable(attrEntityList)
                .orElseGet(ArrayList::new)
                .stream()
                .map(AttrEntity::getAttrId)
                .distinct()
                .collect(Collectors.toList());
        return searchAttrIds;
    }

}